//
//  NRWBrandSeriesVC.m
//  NRWMall
//
//  Created by 邢毅 on 2018/5/23.
//  Copyright © 2018年 com.anpxd. All rights reserved.
//

#import "NRWBrandSeriesVC.h"
#import "NRWBrandTableView.h"
#import "NRWIndexBarView.h"
#import "NRWGetBrandRequest.h"
#import "NRWBrandModel.h"
#import "NRWSeriesModel.h"
#import "NRWSeriesTableView.h"
#import "NRWGetSeriesRequest.h"
#import "NRWBuyCarConditionModel.h"

@interface NRWBrandSeriesVC ()<NRWIndexBarViewDelegate, NRWBrandTableViewDelegate, NRWSeriesTableViewDelegate>
///索引条文字提示
@property (strong, nonatomic) UILabel *indexBarLabel;
///indexBarLabel是否正在显示状态下
@property (assign, nonatomic) BOOL isIndexBarLabelShow;
///车辆品牌请求
@property (strong, nonatomic) NRWGetBrandRequest *brandRequset;
///车系请求
@property (strong, nonatomic) NRWGetSeriesRequest *seriesRequest;
///品牌model
@property (strong, nonatomic) NRWBrandModel *brandModel;
///车系model
@property (strong, nonatomic) NRWSeriesModel *seriseModel;
///车系tableView
@property (strong, nonatomic) NRWSeriesTableView *seriesTableView;

@end

@implementation NRWBrandSeriesVC
{
    NRWBrandTableView *_brandView;
    NRWIndexBarView *_indexView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationView.title = @"品牌车系";
    
    [self setupUI];
    
    [SVProgressHUD show];
    [self.brandRequset start];
}

- (void)setupUI {
    
    _brandView = [[NRWBrandTableView alloc] init];
    _brandView.frame = CGRectMake(0, getNavHeight(), SCREEN_WIDTH(), SCREEN_HEIGHT() - getTabBarHeight());
    _brandView.delegate_brand = self;
    _brandView.selectBrandId = self.selectBrandModel.brandId;
    [self.view addSubview:_brandView];
    
    _indexView = [[NRWIndexBarView alloc] init];
    _indexView.delegate = self;
    [self.view addSubview:_indexView];
    
    if (isNotEmptyString(self.selectBrandModel.seriesId)) {
        
        [self.seriesTableView showForView:self.view];
        self.seriesTableView.selectId = self.selectBrandModel.seriesId;
        self.seriesRequest.brandId = _brandView.selectBrandId;
        [self.seriesRequest start];
    }
}

#pragma mark - networkDelegate
- (void)requestFinished:(__kindof YTKBaseRequest *)request {
    
    [SVProgressHUD dismiss];
    
    if (request == self.brandRequset) {
        
        self.brandModel = [NRWBrandModel mj_objectWithKeyValues:request.responseObject];
        
        _indexView.indexTitleArr = self.brandModel.indexBarInitialArr;
        _brandView.model = self.brandModel;
        return;
    }
    
    if (request == self.seriesRequest) {
        
        self.seriseModel = [NRWSeriesModel mj_objectWithKeyValues:self.seriesRequest.responseObject];
        self.seriesTableView.model = self.seriseModel;
    }
}

- (void)requestFailed:(__kindof YTKBaseRequest *)request {
    
	[NRWMessageHUD showMessage:@"服务繁忙, 请稍后再试" forTime:1.5 withRemindId:remindFail];
}

#pragma mark - NRWIndexBarViewDelegate
- (void)indexBarView:(NRWIndexBarView *)view didSelectAtIndex:(NSInteger)index title:(NSString *)title {
    
    self.indexBarLabel.text = title;
    [self.indexBarLabel sizeToFit];
    self.indexBarLabel.frame = CGRectMake(SCREEN_WIDTH() - _indexBarLabel.viewWidth - 70, 0, _indexBarLabel.viewWidth, _indexBarLabel.viewHeight);
    self.indexBarLabel.setCenterY((SCREEN_HEIGHT() - getNavHeight()) / 2);
    
    [_brandView scrollToSection:index + 1];
}

- (void)indexBarViewDidStartTouch:(NRWIndexBarView *)view {
    
    self.indexBarLabel.hidden = NO;
}

- (void)indexBarViewDidEndTouch:(NRWIndexBarView *)view {
    
    self.indexBarLabel.hidden = YES;
}

#pragma mark - NRWBrandTableViewDelegate
- (void)brandTableView:(NRWBrandTableView *)view didSelectedAtIndexPath:(NSIndexPath *)indexPath {
    
    // 选择不限车系, 直接返回
    if (indexPath.section == 0 && indexPath.row == 0) {
        [self callDelegateMethodWithSelectBrandId:nil selectBrandName:nil selectSeriesId:nil selectSeriesName:nil];
        return;
    }
    
    NRWBrandInfoModel *infoModel = self.brandModel.result[indexPath.section - 1].brandInfo[indexPath.row];
    self.seriesTableView.brandName = infoModel.brandName;
    self.seriesTableView.brandLogo = infoModel.brandLogo;
    
    // 如果点击同当前选中状态, 直接弹出车系列表
    if ([_brandView.selectBrandId isEqualToString:infoModel.brandId]) {
        if (self.seriesTableView.isShow == NO) {
            [self.seriesTableView showForView:self.view];
            self.seriesTableView.selectId = self.selectBrandModel.seriesId;
            self.seriesRequest.brandId = _brandView.selectBrandId;
            // 判断车系model是否存在. 减少数据转模型的逻辑
            if (self.seriseModel.result.count == 0) {
                [self.seriesRequest start];
            } else {
                self.seriesTableView.model = self.seriseModel;
            }
        }
        return;
    }

    self.selectBrandModel.brandId = infoModel.brandId;
    self.selectBrandModel.brandName = infoModel.brandName;
    
    //发送请求
    self.seriesRequest.brandId = infoModel.brandId;
    [self.seriesRequest start];
    [self.seriesTableView showForView:self.view];
    
    //设置选中状态
    _brandView.selectBrandId = infoModel.brandId;
    self.seriesTableView.selectId = nil;
    _brandView.model = self.brandModel;
}

- (void)brandTableViewWillBeginDragging:(NRWBrandTableView *)view {
    
    [self.seriesTableView dismiss];
}

#pragma mark - NRWSeriseTableViewDelegate
- (void)seriesTableView:(NRWSeriesTableView *)view didSelectedAtIndexPath:(NSIndexPath *)indexPath {
    
    NRWSeriesItemModel *seriesItemModel = self.seriseModel.result[indexPath.row];
    
    if (indexPath.section == 0 && indexPath.row == 0) {//所有
        
        return;
    }
    
    if (indexPath.section == 1 && indexPath.row == 0) {
        [self callDelegateMethodWithSelectBrandId:self.selectBrandModel.brandId selectBrandName:self.selectBrandModel.brandName selectSeriesId:nil selectSeriesName:nil];
        return;
    }
    
    //设置选中状态
    self.seriesTableView.selectId = seriesItemModel.seriesId;
    self.seriesTableView.model = self.seriseModel;
    self.selectBrandModel.seriesId = seriesItemModel.seriesId;
    self.selectBrandModel.seriesName = seriesItemModel.seriesName;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self callDelegateMethodWithSelectBrandId:self.selectBrandModel.brandId selectBrandName:self.selectBrandModel.brandName selectSeriesId:self.selectBrandModel.seriesId selectSeriesName:self.selectBrandModel.seriesName];
    });
}

- (void)callDelegateMethodWithSelectBrandId:(NSString *)selectBrandId selectBrandName:(NSString *)selectBrandName selectSeriesId:(NSString *)selectSeriesId selectSeriesName:(NSString *)selectSeriesName {
    
    if ([self.delegate respondsToSelector:@selector(brandSeriesVC:selectBrandId:selectBrandName:selectSeriesId:selectSeriesName:)]) {
        
        [self.delegate brandSeriesVC:self selectBrandId:selectBrandId selectBrandName:selectBrandName selectSeriesId:selectSeriesId selectSeriesName:selectSeriesName];
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - getter
- (UILabel *)indexBarLabel {
    
    if (_indexBarLabel == nil) {
        _indexBarLabel = [[UILabel alloc] init];
        _indexBarLabel.font = getThemeFont(40);
        _indexBarLabel.textColor = mainColor();
        _indexBarLabel.backgroundColor = [UIColor clearColor];
        [self.view addSubview:_indexBarLabel];
    }
    return _indexBarLabel;
}

- (NRWGetBrandRequest *)brandRequset {
    
    if (_brandRequset == nil) {
        _brandRequset = [[NRWGetBrandRequest alloc] init];
        _brandRequset.delegate = self;
    }
    return _brandRequset;
}

- (NRWGetSeriesRequest *)seriesRequest {
    
    if (_seriesRequest == nil) {
        _seriesRequest = [[NRWGetSeriesRequest alloc] init];
        _seriesRequest.delegate = self;
    }
    return _seriesRequest;
}

- (NRWSeriesTableView *)seriesTableView {
    
    if (_seriesTableView == nil) {
        _seriesTableView = [[NRWSeriesTableView alloc] init];
        _seriesTableView.delegate_series = self;
        _seriesTableView.frame = CGRectMake(SCREEN_WIDTH() * 0.26, getNavHeight(), SCREEN_WIDTH() * 0.74, SCREEN_HEIGHT() - getNavHeight());
    }
    return _seriesTableView;
}

- (NRWBuyCarBrandModel *)selectBrandModel {
    
    if (_selectBrandModel == nil) {
        _selectBrandModel = [[NRWBuyCarBrandModel alloc] init];
    }
    return _selectBrandModel;
}

@end
